namespace Webinator.Impls.WebDriver
{
    using System;
    using System.Diagnostics.CodeAnalysis;
    using System.Drawing.Imaging;
    using System.IO;

    using Interfaces;
    using OpenQA.Selenium;

    [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1600:ElementsMustBeDocumented", Justification = "Reviewed. Suppression is OK here.")]
    internal class WebDriverLogUtil : LogUtil
    {
        private readonly ICore _core;

        public WebDriverLogUtil(ICore core) 
            : base(core)
        {
            _core = core;
        }

        public override void LogScreenshot()
        {
            if (!_core.Config.LogScreenshots)
            {
                return;
            }

            try
            {
                var img = ((ITakesScreenshot)_core.GetCore()).GetScreenshot();
                img.SaveAsFile(Path.Combine(_core.Config.LogDirectory, string.Format("{0}.jpg", ScreenshotCounter.ToString("D5"))), ImageFormat.Jpeg);
                ScreenshotCounter++;
            }
            catch (Exception ex)
            {
                Append("Errors", "Unable to capture screenshot");
                Append("Errors", ex.ToString());
            }
        }

        public override void DisplayMessageInBrowser(string message, int interval = 2500)
        {
            MessagePoster messagePoster = new MessagePoster(_core);
            messagePoster.DisplayMessageInBrowser(message, interval);
        }
    }
}
